<?php

namespace App\Http\Controllers\H5;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Params\H5\CommentListParams;
use App\Params\H5\GoodsListParams;
use App\Services\Goods\CategoryService;
use App\Services\Goods\GoodsService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;

/**
 * 商品控制器
 *
 * @author WangBin
 * @date 2021-09-15
 * @package App\Http\Controllers\H5
 */
class GoodsController extends BaseController
{
    use VerifyRequestParam;

    /**
     * 获取商品分类（树形）
     *
     * @param Request $request
     * @return
     */
    public function getCategoryList(Request $request)
    {
        // 获取分类列表（含父子关系）
        $ret = CategoryService::getInstance()->getH5Tree();
        return $this->success($ret);
    }

    /**
     * 获取商品详情
     *
     * @throws
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGoods()
    {
        $goodsId = $this->verifyString('goods_id');
        $ret = GoodsService::getInstance()->getH5Goods($goodsId);
        if(empty($ret)) {
            $this->fail(ResponseCode::GOODS_NOT_FOUND);
        }
        return $this->success($ret);
    }

    /**
     * 获取商品列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGoodsList()
    {
        $filter = GoodsListParams::create();
        $ret = GoodsService::getInstance()->getH5GoodsList($filter);
        return $this->success($ret);
    }

    /**
     * 收藏商品
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function addFavorite(Request $request)
    {
        $goodsId = $request->input('goods_id', 0);
        $goodsIds = $request->input('goods_ids', null);
        $customerId = $request->attributes->get('customer_id', 0);
        if(is_array($goodsIds)) {
            // 批量取消收藏
            $ret = GoodsService::getInstance()->batchCancelFavorite($customerId, $goodsIds);
            if($ret) {
                return $this->success();
            }
            return $this->fail(ResponseCode::GOODS_ADD_FAVORITE_FAIL);
        }
        $ret = GoodsService::getInstance()->addFavorite($customerId, $goodsId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_ADD_FAVORITE_FAIL);
    }

    /**
     * 商品取消收藏
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function cancelFavorite(Request $request)
    {
        $goodsId = $request->input('goods_id', 0);
        $customerId = $request->attributes->get('customer_id', 0);
        $ret = GoodsService::getInstance()->cancelFavorite($customerId, $goodsId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_CANCEL_FAVORITE);
    }

    /**
     * 收藏列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getFavoriteList(Request $request)
    {
        $filter = [
            'customer_id' => $request->attributes->get('customer_id', 0),
            'limit' => $request->input('per_page', 10),
            'page' => $request->input('page', 1)
        ];
        $ret = GoodsService::getInstance()->getFavoriteList($filter);
        return $this->success($ret);
    }

    /**
     * 获取商品服务列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getServiceList()
    {
        return $this->success(GoodsService::getInstance()->getServiceList());
    }

    /**
     * 获取商品评论列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCommentList()
    {
        $param = CommentListParams::create();
        $data = GoodsService::getInstance()->getCommentList($param);
        return $this->success($data);
    }

    /**
     * 获取商品评论统计
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function getCommentStat()
    {
        $goodsId = $this->verifyInteger('goods_id');
        $ret = GoodsService::getInstance()->getCommentStat($goodsId);
        return $this->success($ret);
    }
}
